package com.p2p.common.token.impl;

import com.alibaba.druid.util.StringUtils;
import com.p2p.common.token.TokenService;
import com.p2p.common.utils.TokenUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Created by lizhuolun on 2016/11/17.
 */

@Service
public class TokenServiceImpl implements TokenService {

    private static final Logger logger = LoggerFactory.getLogger(TokenServiceImpl.class);
    private static final String TOKEN = "p2p:token:";

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String createToken(Long key, String redisKey) {
        if (StringUtils.isEmpty(redisKey)) {
            redisKey = TOKEN;
        }
        String token = null;
        if (key == 0 || key == null) {
            return token;
        }
        token = TokenUtils.generateToken(key);

        if (!StringUtils.isEmpty(token)) {
            try {
                int expiredDays = 1;
                redisTemplate.opsForValue().set(redisKey + key + "", token, expiredDays, TimeUnit.DAYS);
                logger.info("生成了token==" + token);
            } catch (Exception e) {
                logger.error("[" + this.getClass().getName() + "]", e);
            }
        }
        return token;
    }

    @Override
    public Long getKeyByToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        long key = TokenUtils.decryptToken(token);
        if (key <= 0) {
            return null;
        }
        try {
            if (StringUtils.equals(token, redisTemplate.opsForValue().get(TOKEN + key + ""))) {
                return key;
            }
        } catch (Exception e) {
            logger.error("[" + this.getClass().getName() + "]", e);
        }
        return null;
    }

    @Override
    public String getTokenByKey(Long key) {
        String token = null;
        if (key != 0 || key != null) {
            try {
                token = redisTemplate.opsForValue().get(TOKEN + key + "");
            } catch (Exception e) {
                logger.error("[" + this.getClass().getName() + "]", e);
            }
            if (!StringUtils.isEmpty(token)) {
                long tempKey = TokenUtils.decryptToken(token);
                if (tempKey <= 0) {
                    return null;
                }
                if (tempKey != key) {
                    return null;
                }
            }
        }
        return token;
    }

    @Override
    public boolean destroyToken(String accessToken) {
        boolean rt = false;
        if (StringUtils.isEmpty(accessToken)) {
            return rt;
        }
        long key = TokenUtils.decryptToken(accessToken);
        if (key <= 0) {
            return rt;
        }
        try {
            if (StringUtils.equals(accessToken, redisTemplate.opsForValue().get(TOKEN + key + ""))) {
                redisTemplate.opsForValue().getOperations().delete(TOKEN + key + "");
                rt = true;
            }
        } catch (Exception e) {
            logger.error("[" + this.getClass().getName() + "]", e);
            rt = false;
        }
        return rt;
    }
}
